package com.gobang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gobang.domain.entity.GameRoom;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 游戏房间数据访问层
 * 提供游戏房间相关的数据库操作
 * 
 * @author System
 * @since 1.0.0
 */
@Mapper
public interface GameRoomMapper extends BaseMapper<GameRoom> {

    /**
     * 根据玩家ID查询正在进行的游戏
     *
     * @param playerId 玩家ID
     * @return 游戏房间信息
     */
    @Select("SELECT * FROM game_rooms WHERE (black_player_id = #{playerId} OR white_player_id = #{playerId}) " +
            "AND game_status = 'PLAYING'")
    GameRoom findActiveGameByPlayerId(@Param("playerId") Long playerId);

    /**
     * 更新游戏状态
     *
     * @param roomId 房间ID
     * @param gameStatus 游戏状态
     * @return 更新行数
     */
    @Update("UPDATE game_rooms SET game_status = #{gameStatus} WHERE id = #{roomId}")
    int updateGameStatus(@Param("roomId") String roomId, @Param("gameStatus") String gameStatus);

    /**
     * 更新游戏结果
     *
     * @param roomId 房间ID
     * @param winnerId 获胜者ID
     * @param gameStatus 游戏状态
     * @param endReason 结束原因
     * @return 更新行数
     */
    @Update("UPDATE game_rooms SET winner_id = #{winnerId}, game_status = #{gameStatus}, " +
            "end_reason = #{endReason}, ended_at = NOW() WHERE id = #{roomId}")
    int updateGameResult(@Param("roomId") String roomId, 
                        @Param("winnerId") Long winnerId, 
                        @Param("gameStatus") String gameStatus,
                        @Param("endReason") String endReason);

    /**
     * 更新当前回合
     *
     * @param roomId 房间ID
     * @param currentTurn 当前回合
     * @return 更新行数
     */
    @Update("UPDATE game_rooms SET current_turn = #{currentTurn} WHERE id = #{roomId}")
    int updateCurrentTurn(@Param("roomId") String roomId, @Param("currentTurn") String currentTurn);

    /**
     * 增加总步数
     *
     * @param roomId 房间ID
     * @return 更新行数
     */
    @Update("UPDATE game_rooms SET total_moves = total_moves + 1 WHERE id = #{roomId}")
    int incrementTotalMoves(@Param("roomId") String roomId);

    /**
     * 开始游戏
     *
     * @param roomId 房间ID
     * @return 更新行数
     */
    @Update("UPDATE game_rooms SET game_status = 'PLAYING', started_at = NOW() " +
            "WHERE id = #{roomId} AND game_status = 'WAITING'")
    int startGame(@Param("roomId") String roomId);

    /**
     * 根据玩家ID查询历史游戏记录
     *
     * @param playerId 玩家ID
     * @return 游戏房间列表
     */
    @Select("SELECT * FROM game_rooms WHERE (black_player_id = #{playerId} OR white_player_id = #{playerId}) " +
            "AND game_status IN ('FINISHED', 'CANCELLED') ORDER BY ended_at DESC")
    List<GameRoom> findHistoryGamesByPlayerId(@Param("playerId") Long playerId);

    /**
     * 根据房间ID查询房间信息
     *
     * @param roomId 房间ID
     * @return 游戏房间信息
     */
    @Select("SELECT * FROM game_rooms WHERE id = #{roomId}")
    GameRoom findByRoomId(@Param("roomId") String roomId);
} 